﻿using System;
using System.IO;
using Microsoft.Win32;
using Microsoft.WowAddonStudio.Services;

namespace Microsoft.WowAddonStudio.Project.Services
{
    public class WowLocatorService : IWowLocatorService
    {
        private const string wowPathSubKeyName = @"SOFTWARE\Blizzard Entertainment\World of Warcraft";
        private const string wowPathName = "InstallPath";

        private readonly WowProjectPackage wowProjectPackage;

        /// <summary>
        /// Initializes a new instance of the <see cref="WowLocatorService"/> class.
        /// </summary>
        /// <param name="wowProjectPackage">The wow project package.</param>
        public WowLocatorService(WowProjectPackage wowProjectPackage)
        {
            if (wowProjectPackage == null)
                throw new ArgumentNullException("wowProjectPackage");

            this.wowProjectPackage = wowProjectPackage;
        }

        /// <summary>
        /// Gets a value indicting whether a World of Warcraft installation was detected.
        /// </summary>
        /// <value></value>
        public bool IsWowInstalled
        {
            get
            {
                return this.GetWowBasePath() != null;
            }
        }

        /// <summary>
        /// Gets the base path to the World of Warcraft installation.
        /// </summary>
        /// <returns>The base path to World of Warcraft.</returns>
        public string GetWowBasePath()
        {
/*
            var settingsService = wowProjectPackage.GetService<IAddonStudioSettings>();
            if (settingsService != null && !String.IsNullOrEmpty(settingsService.WowBasePath) && Directory.Exists(settingsService.WowBasePath))
                return settingsService.WowBasePath;
*/

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(wowPathSubKeyName))
            {
                if (key != null)
                    return (string) key.GetValue(wowPathName, null);
            }

            return null;
        }

        /// <summary>
        /// Gets the latest World of Warcraft Interface version.
        /// </summary>
        /// <returns>The latest World of Warcraft interface version.</returns>
        public string GetWowInterface()
        {
            return "30000";
        }
    }
}